﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;

namespace AnyExeTo.WindowsService
{
    /// <summary>
    /// 任意可执行程序项目安装器
    /// </summary>
    [RunInstaller(true)]
    public partial class AnyExeProjectInstaller : System.Configuration.Install.Installer
    {
        /// <summary>
        /// 初始化 任意可执行程序项目安装器
        /// </summary>
        public AnyExeProjectInstaller()
        {
            InitializeComponent();
            Set();
        }

        /// <summary>
        /// 设置
        /// </summary>
        public void Set()
        {
            var options = ServiceOptions.LoadFromFile();

            if (!string.IsNullOrWhiteSpace(options.ServiceName))
            {
                serviceInstallerForAnyExe.ServiceName = options.ServiceName;
            }

            if (string.IsNullOrWhiteSpace(options.DisplayName))
            {
                if (!string.IsNullOrWhiteSpace(options.ServiceName))
                {
                    serviceInstallerForAnyExe.DisplayName = options.ServiceName;
                }
            }
            else
            {
                serviceInstallerForAnyExe.DisplayName = options.DisplayName;
            }

            if (!string.IsNullOrWhiteSpace(options.Description))
            {
                serviceInstallerForAnyExe.Description = options.Description;
            }

            if (!string.IsNullOrWhiteSpace(options.StartType))
            {
                switch (options.StartType)
                {
                    case "Automatic":
                        serviceInstallerForAnyExe.StartType = ServiceStartMode.Automatic;
                        break;
                    case "Manual":
                        serviceInstallerForAnyExe.StartType = ServiceStartMode.Manual;
                        break;
                    case "Disabled":
                        serviceInstallerForAnyExe.StartType = ServiceStartMode.Disabled;
                        break;
                    default:
                        break;
                }
            }

            if (!string.IsNullOrWhiteSpace(options.Account))
            {
                switch (options.Account)
                {
                    case "LocalService":
                        serviceProcessInstallerForAnyExe.Account = ServiceAccount.LocalService;
                        break;
                    case "NetworkService":
                        serviceProcessInstallerForAnyExe.Account = ServiceAccount.NetworkService;
                        break;
                    case "LocalSystem":
                        serviceProcessInstallerForAnyExe.Account = ServiceAccount.LocalSystem;
                        break;
                    case "User":
                        serviceProcessInstallerForAnyExe.Account = ServiceAccount.User;
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
